<template>
	<view class="coupon" :style="'background-color:'+bgColor+';margin-top:' + mbConfig +'rpx;'" v-if="couponList.length">
		<scroll-view scroll-x="true" style="white-space: nowrap; vertical-align: middle;" show-scrollbar="false">

			<view class="wrapper">
			
			<view class="item" :style="item.is_use?'':'background-color:'+ themeColor +';'" style="margin-right: 20rpx;" v-for="(item,index) in couponList"
			 :key="index" hover-class="none">
				<view class="itemCon acea-row row-between-wrapper">
					<view class="text">
						<view class="money"><text>¥</text>{{item.coupon_price}}</view>
						<view class="info">满{{item.use_min_price}}元可用</view>
					</view>
					<view class="bnt" v-if="item.is_use===true"><text>已 领 取</text></view>
					<view class="bnt" v-else-if="item.is_use===false" @click="receiveCoupon(item)"><text>立 即 领 取</text></view>
					<view class="bnt" v-else-if="item.is_use===2"><text>已 过 期</text></view>
				</view>
			</view>
			<navigator url="/pages/users/user_get_coupon/index" class="more-box" hover-class="none">
				<view class="txt">更多</view>
				<image src="/static/images/mores.png"></image>
			</navigator>
			</view>	
		</scroll-view>
		<!-- #ifdef MP -->
		<authorize @onLoadFun="onLoadFun" :isAuto="isAuto" :isShowAuth="isShowAuth" @authColse="authColse" :isGoIndex="false"></authorize>
		<!-- #endif -->
	</view>
</template>

<script>
	import {
		getCoupons,
		setCouponReceive
	} from '@/api/api.js';
	// #ifdef MP
	import authorize from '@/components/Authorize';
	// #endif
	import {
		mapGetters
	} from "vuex";
	import {
		toLogin
	} from '@/libs/login.js';
	export default {
		name: 'coupon',
		props: {
			dataConfig: {
				type: Object,
				default: () => {}
			}
		},
		computed: mapGetters(['isLogin']),
		components: {
			// #ifdef MP
			authorize
			// #endif
		},
		data() {
			return {
				isAuto: false, //没有授权的不会自动授权
				isShowAuth: false, //是否隐藏授权
				couponList: [],
				bgColor: this.dataConfig.bgColor.color[0].item,
				themeColor: this.dataConfig.themeColor.color[0].item,
				mbConfig: this.dataConfig.mbConfig.val
			};
		},
		created() {},
		mounted() {
			this.getCoupon();
		},
		methods: {
			onLoadFun(){
				
			},
			getCoupon: function() {
				let that = this;
				let limit = that.$config.LIMIT;
				getCoupons({
					page: 1,
					limit: limit
				}).then(res => {
					that.$set(that, 'couponList', res.data);
				}).catch(err => {
					return that.$util.Tips({
						title: err
					});
				});
			},
			receiveCoupon: function(item) {
				let that = this;
				if (!that.isLogin) {
					// #ifdef H5 || APP-PLUS
					toLogin();
					// #endif 
					// #ifdef MP
					that.$set(that, 'isAuto', true);
					that.$set(that, 'isShowAuth', true)
					// #endif
				} else {
					setCouponReceive(item.id)
						.then(function() {
							item.is_use = true;
							that.$set(that, 'couponList', that.couponList);
							that.$util.Tips({
								title: "领取成功"
							});
						})
						.catch(function(err) {
							that.$util.Tips({
								title: err
							});
						});
				}
			}
		}
	}
</script>

<style lang="scss">
	.coupon {
		background-color: #fff;
		padding: 20rpx;

		.item {
			display: flex;
			width: 304rpx;
			height: 122rpx;
			background-color: #DDDDDD;
			border-radius: 8rpx;
			color: #fff;
			position: relative;
			display: inline-block;
			flex-shrink: 0;
			&::before {
				position: absolute;
				content: ' ';
				width: 20rpx;
				height: 20rpx;
				border-radius: 50%;
				background-color: #fff;
				right: 52rpx;
				top: -10rpx;
			}

			&::after {
				position: absolute;
				content: ' ';
				width: 20rpx;
				height: 20rpx;
				border-radius: 50%;
				background-color: #fff;
				right: 52rpx;
				bottom: -10rpx;
			}

			.itemCon {
				height: 100%;
				width: 100%;

				.text {
					width: 240rpx;

					.money {
						font-size: 48rpx;
						text-align: center;

						text {
							font-size: 24rpx;
						}
					}

					.info {
						font-size: 24rpx;
						text-align: center;
					}
				}

				.bnt {
					position: relative;
					height: 100%;
					font-size: 20rpx;
					display: block;
					writing-mode: vertical-lr;
					line-height: 1.2;
					width: 64rpx;
					border-left: 1px dashed #fff;
					text{
						position: absolute;
						left: 56%;
						top: 50%;
						transform: translate(-50%,-50%);
					}
				}
			}
		}

		.wrapper {
			display: flex;
		}

		.more-box {
			display: flex;
			flex-direction: column;
			align-items: center;
			justify-content: center;
			background-color: #fff;
			border-radius: 16rpx;
			padding: 0 10px;
			height: 122rpx;
			image {
				width: 20rpx;
				height: 20rpx;
				margin-top: 10rpx;
				margin: 10rpx 0 0 5rpx;
			}

			.txt {
				display: block;
				writing-mode: vertical-lr;
				font-size: 20rpx;
				line-height: 1.2;
			}
		}
	}
</style>
